home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / archiver / unix / unz50p1.zoo / extract.c < prev    next >
C/C++ Source or Header  |  1993-01-11  |  39KB  |  1,077 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.   extract.c
  4.  
  5.   This file contains the high-level routines ("driver routines") for extrac-
  6.   ting and testing zipfile members.  It calls the low-level routines in files
  7.   explode.c, inflate.c, unreduce.c and unshrink.c.
  8.  
  9.   ---------------------------------------------------------------------------*/
  10.  
  11.  
  12. #include "unzip.h"
  13. #ifdef  MSWIN
  14. #  include "wizunzip.h"
  15. #  include "replace.h"
  16. #endif /* MSWIN */
  17.  
  18.  
  19. /************************************/
  20. /*  Extract Local Prototypes, etc.  */
  21. /************************************/
  22.  
  23. static int store_info __((void));
  24. static int extract_or_test_member __((void));
  25. #ifdef CRYPT
  26.    static int decrypt_member __((void));
  27.    static int testp __((byte *hdr));
  28. #endif
  29.  
  30. static byte *mem_i_buffer;
  31. static byte *mem_o_buffer;
  32. static ULONG mem_i_size, mem_i_offset;
  33. static ULONG mem_o_size, mem_o_offset;
  34.  
  35. static char *VersionMsg =
  36.   " skipping: %-22s  need %s compat. v%u.%u (can do v%u.%u)\n";
  37. static char *ComprMsg =
  38.   " skipping: %-22s  compression method %d\n";
  39. static char *FilNamMsg =
  40.   "%s:  bad filename length (%s)\n";
  41. static char *ExtFieldMsg =
  42.   "%s:  bad extra field length (%s)\n";
  43. static char *OffsetMsg =
  44.   "file #%d:  bad zipfile offset (%s)\n";
  45.  
  46.  
  47.  
  48.  
  49.  
  50. /**************************************/
  51. /*  Function extract_or_test_files()  */
  52. /**************************************/
  53.  
  54. int extract_or_test_files()    /* return PK-type error code */
  55. {
  56.     char **fnamev;
  57.     byte *cd_inptr;
  58.     int cd_incnt, error, error_in_archive=0;
  59.     int renamed, query, len, filnum=(-1), blknum=0;
  60. #ifdef OS2
  61.     extern int longname;  /* from os2unzip.c */
  62. #endif
  63.     UWORD i, j, members_remaining, num_skipped=0, num_bad_pwd=0;
  64.     longint cd_bufstart, bufstart, inbuf_offset, request;
  65.     min_info info[DIR_BLKSIZ];
  66.  
  67.  
  68. /*---------------------------------------------------------------------------
  69.     The basic idea of this function is as follows.  Since the central di-
  70.     rectory lies at the end of the zipfile and the member files lie at the
  71.     beginning or middle or wherever, it is not very desirable to simply
  72.     read a central directory entry, jump to the member and extract it, and
  73.     then jump back to the central directory.  In the case of a large zipfile
  74.     this would lead to a whole lot of disk-grinding, especially if each mem-
  75.     ber file is small.  Instead, we read from the central directory the per-
  76.     tinent information for a block of files, then go extract/test the whole
  77.     block.  Thus this routine contains two small(er) loops within a very
  78.     large outer loop:  the first of the small ones reads a block of files
  79.     from the central directory; the second extracts or tests each file; and
  80.     the outer one loops over blocks.  There's some file-pointer positioning
  81.     stuff in between, but that's about it.  Btw, it's because of this jump-
  82.     ing around that we can afford to be lenient if an error occurs in one of
  83.     the member files:  we should still be able to go find the other members,
  84.     since we know the offset of each from the beginning of the zipfile.
  85.  
  86.     Begin main loop over blocks of member files.  We know the entire central
  87.     directory is on this disk:  we would not have any of this information un-
  88.     less the end-of-central-directory record was on this disk, and we would
  89.     not have gotten to this routine unless this is also the disk on which
  90.     the central directory starts.  In practice, this had better be the ONLY
  91.     disk in the archive, but maybe someday we'll add multi-disk support.
  92.   ---------------------------------------------------------------------------*/
  93.  
  94.     pInfo = info;
  95.     members_remaining = ecrec.total_entries_central_dir;
  96.  
  97.     while (members_remaining) {
  98.         j = 0;
  99.  
  100.         /*
  101.          * Loop through files in central directory, storing offsets, file
  102.          * attributes, and case-conversion flags until block size is reached.
  103.          */
  104.  
  105.         while (members_remaining && (j < DIR_BLKSIZ)) {
  106.             --members_remaining;
  107.             pInfo = &info[j];
  108.  
  109.             if (readbuf(sig, 4) <= 0) {
  110.                 error_in_archive = 51;  /* 51:  unexpected EOF */
  111.                 members_remaining = 0;  /* ...so no more left to do */
  112.                 break;
  113.             }
  114.             if (strncmp(sig, central_hdr_sig, 4)) {  /* just to make sure */
  115.                 fprintf(stderr, CentSigMsg, j);  /* sig not found */
  116.                 fprintf(stderr, ReportMsg);   /* check binary transfers */
  117.                 error_in_archive = 3;   /* 3:  error in zipfile */
  118.                 members_remaining = 0;  /* ...so no more left to do */
  119.                 break;
  120.             }
  121.             /* process_cdir_file_hdr() sets pInfo->hostnum, pInfo->lcflag */
  122.             if ((error = process_cdir_file_hdr()) != 0) {
  123.                 error_in_archive = error;   /* only 51 (EOF) defined */
  124.                 members_remaining = 0;  /* ...so no more left to do */
  125.                 break;
  126.             }
  127.             if ((error = do_string(crec.filename_length, FILENAME)) != 0) {
  128.                 if (error > error_in_archive)
  129.                     error_in_archive = error;
  130.                 if (error > 1) {  /* fatal:  no more left to do */
  131.                     fprintf(stderr, FilNamMsg, filename, "central");
  132.                     members_remaining = 0;
  133.                     break;
  134.                 }
  135.             }
  136.             if ((error = do_string(crec.extra_field_length, EXTRA_FIELD)) != 0)
  137.             {
  138.                 if (error > error_in_archive)
  139.                     error_in_archive = error;
  140.                 if (error > 1) {  /* fatal */
  141.                     fprintf(stderr, ExtFieldMsg, filename, "central");
  142.                     members_remaining = 0;
  143.                     break;
  144.                 }
  145.             }
  146.             if ((error = do_string(crec.file_comment_length, SKIP)) != 0) {
  147.                 if (error > error_in_archive)
  148.                     error_in_archive = error;
  149.                 if (error > 1) {  /* fatal */
  150.                     fprintf(stderr, "\n%s:  bad file comment length\n",
  151.                             filename);
  152.                     members_remaining = 0;
  153.                     break;
  154.                 }
  155.             }
  156.             if (process_all_files) {
  157.                 if (store_info())
  158.                     ++num_skipped;
  159.                 else
  160.                     ++j;  /* file is OK: save info[] and continue with next */
  161.             } else {
  162.                 fnamev = fnv;   /* don't destroy permanent filename pointer */
  163.                 for (--fnamev; *++fnamev;)
  164.                     if (match(filename, *fnamev)) {
  165.                         if (store_info())
  166.                             ++num_skipped;
  167.                         else
  168.                             ++j;   /* file is OK */
  169.                         break;  /* found match for filename, so stop looping */
  170.                     } /* end if (match), for-loop (fnamev) */
  171.             } /* end if (process_all_files) */
  172.  
  173.         } /* end while-loop (adding files to current block) */
  174.  
  175.         /* save position in central directory so can come back later */
  176.         cd_bufstart = cur_zipfile_bufstart;
  177.         cd_inptr = inptr;
  178.         cd_incnt = incnt;
  179.  
  180.     /*-----------------------------------------------------------------------
  181.         Second loop:  process files in current block, extracting or testing
  182.         each one.
  183.       -----------------------------------------------------------------------*/
  184.  
  185.         for (i = 0; i < j; ++i) {
  186.             filnum = i + blknum*DIR_BLKSIZ;
  187.             pInfo = &info[i];
  188.             /*
  189.              * if the target position is not within the current input buffer
  190.              * (either haven't yet read far enough, or (maybe) skipping back-
  191.              * ward) skip to the target position and reset readbuf().
  192.              */
  193.             /* LSEEK(pInfo->offset):  */
  194.             request = pInfo->offset + extra_bytes;
  195.             inbuf_offset = request % INBUFSIZ;
  196.             bufstart = request - inbuf_offset;
  197.  
  198.             if (request < 0) {
  199.                 fprintf(stderr, SeekMsg, ReportMsg);
  200.                 error_in_archive = 3;       /* 3:  severe error in zipfile, */
  201.